using System;
using System.Collections.Generic;
using Jalal.Library.DataStructure;
using BCGC.BLL.Helper;
using BCGC.DAL;
using System.Data;
using Jalal.Library.Data;

namespace BCGC.BLL.BusinessObject.Poll
{
    public class PollAnswer
    {

        private int id;
        public int Id
        {
            get
            {
                return id;
            }
        }

        private int questionId;
        public int QuestionId
        {
            get
            {
                return questionId;
            }
        }

        private String answer;
        public String Answer
        {
            get
            {
                return answer;
            }
            set
            {
                answer = value;
            }
        }

        private int vote;
        public int Vote
        {
            get
            {
                return vote;
            }
        }

        private int displayOrder = 0;
        public int DisplayOrder
        {
            get
            {
                return displayOrder;
            }
            set
            {
                displayOrder = value;
            }
        }


        public PollAnswer(String answer)
        {
            this.answer = answer;
            this.displayOrder = 0;
            this.id = 0;
            this.questionId = 0;
            this.vote = 0;
        }

        public PollAnswer(int id, string answer, int displayOrder, int questionId)
        {
            this.answer = answer;
            this.displayOrder = displayOrder;
            this.id = id;
            this.questionId = questionId;
            this.vote = 0;
        }

        public PollAnswer(DataRow row)
        {
            this.answer = (string)row["Answer"];
            this.displayOrder = (int)row["DisplayOrder"];
            this.id = (int)row["Id"];
            this.questionId = (int)row["QuestionId"];
            this.vote = (int)row["Vote"]; 
        }

        public static PollAnswer Create(string answer, int displayOrder, int questionId)
        {
            int answerId = PollData.CreatePollAnswer(answer, displayOrder, questionId);
            PollAnswer pollAnswer = new PollAnswer(answerId, answer, displayOrder, questionId);
            return pollAnswer;
        }

        public static void Delete(PollAnswer answerToDelete)
        {
            throw new System.Exception("Not implemented");
        }

        public static void Delete(List<PollAnswer> answersToDelete)
        {
            throw new System.Exception("Not implemented");
        }

        public static void Delete(List<int> answerIdsToDelete)
        {
            throw new System.Exception("Not implemented");
        }

        public void Delete()
        {
            throw new System.Exception("Not implemented");
        }

        public void Update()
        {
            throw new System.Exception("Not implemented");
        }

        public int CastVote()
        {
            throw new System.Exception("Not implemented");
        }

        public void ResetVote()
        {
            throw new System.Exception("Not implemented");
        }

        public static void UpdateDisplayOrder(List<PollAnswer> orderedAnswers)
        {
            throw new System.Exception("Not implemented");
        }

        public static ReadWriteCustomCollection<PollAnswer> GetAnswersForQuestion(PollQuestion question)
        {
            return GetAnswersForQuestion(question.Id);
        }

        public static ReadWriteCustomCollection<PollAnswer> GetAnswersForQuestion(int questionId)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);

            ReadWriteCustomCollection<PollAnswer> pollAnswers = new ReadWriteCustomCollection<PollAnswer>();

            DataTable result = PollData.GetPollAnswerByQuestionId(questionId);
            DataFactory.GetDataMiner().CommitTransaction();

            return LoadFromDataTable(result);
        }

        public static PollAnswer GetPollAnswerById(int id)
        {
            throw new System.Exception("Not implemented");
        }

        private static ReadWriteCustomCollection<PollAnswer> LoadFromDataTable(DataTable data)
        {
            ReadWriteCustomCollection<PollAnswer> result = new ReadWriteCustomCollection<PollAnswer>();

            if (data != null && data.Rows.Count > 0)
            {
                foreach (DataRow row in data.Rows)
                {
                    result.Add(new PollAnswer(row));
                }
            }
            return result;
        }
    }
}
